<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <pre>
      // 1. 实现树形结构, pid就是每个成员的父级
      let data = [
        { id: 1, pid: 0 },
        { id: 2, pid: 1 },
        { id: 3, pid: 0 },
        { id: 4, pid: 2 },
      ];
      function buildTree(data, pid) {
        let tree = [];
        for (let item of data) {
          if (item.pid === pid) {
            let children = buildTree(data, item.id);
            if (children.length > 0) {
              item.children = children;
            }
            tree.push(item);
          }
        }
        return tree;
      }
      let tree = buildTree(data, 0);
      console.log(tree);

      // 2. 不修改代码的情况下, 实现修改 obj 的内容
      const test = (() => {
        let obj = {
          a: "变量a",
          b: "变量b",
        };
        return {
          get: (k) => {
            return obj[k];
          },
        };
      })();
      Object.defineProperty(Object.prototype, "abc", {
        get() {
          return this;
        },
      });
      let testFn = test.get("abc");
      testFn.c = "变量c";
      console.log(test.get("c"));

      // 3. 得出以下结果, 并且说明原因
      window.number = 2;
      let obj = {
        number: 3,
        db: (function () {
          this.number *= 4;
          return function () {
            this.number *= 5;
          };
        })(),
      };
      let db = obj.db;
      db();
      obj.db();
      console.log(window.number); // 40     因为函数的this指向window,返回值里的函数的this指向函数本身，最后的结果是window.number(2)*4*5=40
      console.log(obj.number); // 15     因为函数的this指向window,返回值里的函数的this指向函数本身，最后的结果是obj.number(3)*5=15

    </pre>
  </body>
</html>
